package com.patrol.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.patrol.dao.WorkDao;
import com.patrol.pojo.Worker;
import com.patrol.service.WorkerService;
import com.patrol.utils.DownloadSettingUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

@Service
public class WorkerServiceImpl implements WorkerService {
    @Resource
    private WorkDao workDao;

    @Override
    public boolean insert(Worker worker) {
        return 1 == workDao.insert(worker);
    }

    @Override
    public boolean delete(int workerId) {
        return 1 == workDao.deleteById(workerId);
    }

    @Override
    public boolean update(Worker worker) {
        return 1 == workDao.updateById(worker);
    }

    @Override
    public Worker select(int id) {
        return workDao.selectById(id);
    }

    @Override
    public Page<Worker> selectPage(long currentPage, long limit) {
        return workDao.selectPage(new Page<>(currentPage, limit), null);
    }

    @Override
    public void exportDataByDate(HttpServletResponse response, String startTime, String endTime) throws IOException {
//        设置下载信息
        DownloadSettingUtil.setting(response, "worker-data");
        QueryWrapper<Worker> qw = new QueryWrapper<>();
        qw.between("date", startTime, endTime);
        List<Worker> list = workDao.selectList(qw);
        EasyExcel.write(response.getOutputStream(), Worker.class).sheet("data").doWrite(list);
    }
}